package com.carmanagement



import static org.springframework.http.HttpStatus.*
import grails.transaction.Transactional

import org.springframework.security.access.annotation.Secured


@Secured(['ROLE_ADMIN', 'ROLE_USER'])
@Transactional
class MaintenanceController {

    static allowedMethods = [save: "POST", update: "PUT", delete: "DELETE"]

    def show(Maintenance maintenanceInstance) {
		respond maintenanceInstance
    }

    def create() {
		def maintenanceInstance = new Maintenance(params)
		def vehicleInstance = Vehicle.findById(params.vehicleId)
		if(!vehicleInstance){
		    noVehicleFound()
		    return
		}
		maintenanceInstance.vehicle = vehicleInstance
		respond maintenanceInstance
    }

    @Transactional
    def save(Maintenance maintenanceInstance) {
		if (maintenanceInstance == null) {
		    notFound()
		    return
		}

		maintenanceInstance.clearErrors()
		Date date = params.date("date", "yyyy/MM/dd")
		Date predictedDate = params.date("predictedDate", "yyyy/MM/dd")
		maintenanceInstance.date = date
		maintenanceInstance.predictedDate = predictedDate
		if (maintenanceInstance.hasErrors()) {
		    respond maintenanceInstance.errors, view:'create', params: params
		    return
		}

		maintenanceInstance.save flush:true

		request.withFormat {
		    form {
			flash.message = message(code: 'default.created.message', args: [
			    message(code: 'maintenanceInstance.label', default: 'Maintenance'),
			    maintenanceInstance.date
			])
			redirect(controller:"vehicle", action:"show", id:maintenanceInstance.vehicle.id)
		    }
		    '*' { respond maintenanceInstance, [status: CREATED] }
		}
    }

    def edit(Maintenance maintenanceInstance) {
		respond maintenanceInstance
    }

    @Transactional
    def update(Maintenance maintenanceInstance) {
		if (maintenanceInstance == null) {
		    notFound()
		    return
		}
		maintenanceInstance.clearErrors()
		Date date = params.date("date", "yyyy/MM/dd")
		Date predictedDate = params.date("predictedDate", "yyyy/MM/dd")
		maintenanceInstance.date = date
		maintenanceInstance.predictedDate = predictedDate
		if (maintenanceInstance.hasErrors()) {
		    respond maintenanceInstance.errors, view:'edit'
		    return
		}

		maintenanceInstance.save flush:true

		request.withFormat {
		    form {
			flash.message = message(code: 'default.updated.message', args: [
			    message(code: 'Maintenance.label', default: 'Maintenance'),
			    maintenanceInstance.id
			])
			redirect maintenanceInstance
		    }
		    '*'{ respond maintenanceInstance, [status: OK] }
		}
    }

    @Transactional
    def delete(Maintenance maintenanceInstance) {

		if (maintenanceInstance == null) {
		    notFound()
		    return
		}

		maintenanceInstance.delete flush:true

		request.withFormat {
		    form {
			flash.message = message(code: 'default.deleted.message', args: [
			    message(code: 'Maintenance.label', default: 'Maintenance'),
			    maintenanceInstance.id
			])
			redirect action:"index", method:"GET"
		    }
		    '*'{ render status: NO_CONTENT }
		}
	    }

	    protected void notFound() {
		request.withFormat {
		    form {
			flash.message = message(code: 'default.not.found.message', args: [
			    message(code: 'maintenanceInstance.label', default: 'Maintenance'),
			    params.id
			])
			redirect action: "index", method: "GET"
		    }
		    '*'{ render status: NOT_FOUND }
		}
    }

    protected void noVehicleFound() {
		request.withFormat {
		    form {
			flash.message = message(code: 'default.not.found.message', args: [
			    message(code: 'vehicleInstance.label', default: 'Vehicle'),
			    params.vehicleId
			])
			redirect controller: "vehicle", action: "index", method: "GET"
		    }
		    '*'{ render status: NOT_FOUND }
		}
    }
}
